package com.fis.vaadin.component;

import com.vaadin.data.Container;
import com.vaadin.data.Container.Filterable;
import com.vaadin.data.Container.ItemSetChangeEvent;
import com.vaadin.data.util.filter.Or;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

import eu.livotov.tpt.i18n.TM;

public class TableContainer extends CustomComponent// implements Handler
{
	
	private TextField txtFilter;
	private Button btnSearch;
	private Table table;
	private HorizontalLayout pnControl;
	private FormLayout pncombo;
	private ComboBox cb = new ComboBox();
	private VerticalLayout mainLayout;
	private String[] filteredColumns = null;
	
	public TableContainer(Table table)
	{
		this.table = table;
		table.setCaption(null);
		table.setColumnWidth(null, 30);
		initLayout();
	}
	
	private void initLayout()
	{
		innitConponent();
		
		HorizontalLayout pnlTop = new HorizontalLayout();
		pnlTop.setWidth("100%");
		Label lbl = new Label();
		lbl.setWidth("100%");
//		pnlTop.addComponent(lbl);
		pnControl = new HorizontalLayout();
		pncombo = new FormLayout();
		pncombo.setMargin(false,false,false,true);
		pncombo.setSpacing(false);
		pnlTop.addComponent(pncombo);
		pnlTop.setComponentAlignment(pncombo, Alignment.MIDDLE_LEFT);
		
		pnlTop.addComponent(pnControl);
		pnlTop.setComponentAlignment(pnControl, Alignment.MIDDLE_RIGHT);
		
		HorizontalLayout layout = new HorizontalLayout();
		Panel pnlSearch = new Panel(layout);
//		pnlSearch.addActionHandler(this);
		pnlSearch.addAction(new Button.ClickShortcut(btnSearch, KeyCode.ENTER));
		pnlSearch.addComponent(txtFilter);
		pnlSearch.addComponent(btnSearch);
		pnlTop.addComponent(pnlSearch);
		pnlSearch.setWidth("227px");
		pnlSearch.addStyleName(Reindeer.PANEL_LIGHT);
		layout.setSpacing(true);
		
//		pnlTop.addComponent(txtFilter);
//		pnlTop.addComponent(btnSearch);
		pnlTop.setSpacing(true);
		pnlTop.setExpandRatio(pncombo, 1.0f);
		
		mainLayout = new VerticalLayout();
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		lbl = new Label();
		lbl.setHeight("2px");
		mainLayout.addComponent(lbl);
		mainLayout.addComponent(pnlTop);
		mainLayout.setComponentAlignment(pnlTop, Alignment.BOTTOM_RIGHT);
		mainLayout.addComponent(table);
		mainLayout.setExpandRatio(table, 1.0f);
		lbl = new Label();
		lbl.setHeight("3px");
		mainLayout.addComponent(lbl);
		
		setCompositionRoot(mainLayout);
	}
	
	public void setActionPanel(HorizontalLayout control)
	{
		pnControl.removeAllComponents();
		pnControl.addComponent(control);
	}
	public void setCombo(ComboBox cb)
	{
		pncombo.removeAllComponents();
		pncombo.addComponent(cb);
	}
	
	private String getFilteredColumnHeader(String[] cols)
	{
		String str = "<strong>";
		for (String col : cols)
		{
			str += table.getColumnHeader(col) + ", ";
		}
		return str.substring(0,str.length()-2) + "</strong>";
	}
	
	private void innitConponent()
	{
		txtFilter = new TextField();
//		txtFilter.setInputPrompt("Nhập dữ liệu");
		btnSearch = new Button(TM.get("main.common.button.search.tooltip"), new Button.ClickListener()
		{
			
			public void buttonClick(ClickEvent event)
			{
				filter();
//				System.out.println("before focusing textfield");
//				txtFilter.focus();
			}
		});
		btnSearch.setDescription(TM.get("main.common.button.search.tooltip"));
//		btnSearch.setClickShortcut(KeyCode.ENTER);
		table.setSizeFull();
		table.setSelectable(true);
		table.setImmediate(true);
		table.setNullSelectionAllowed(false);
		
		table.addListener(new Container.ItemSetChangeListener()
		{
			public void containerItemSetChange(ItemSetChangeEvent event)
			{
				table.setValue(null);
			}
		});
	}

	public void setFilteredColumns(String[] filteredCols)
	{
		if (filteredCols == null || filteredCols.length == 0)
		{
			return;
		}
		
		this.filteredColumns = filteredCols;
		txtFilter.setDescription(TM.get("main.common.table.filter.caption") + " " + getFilteredColumnHeader(filteredColumns));
	}
	
	private void filter()
	{
		Filterable f = (Filterable) table.getContainerDataSource();
		if (filteredColumns == null || filteredColumns.length == 0)
		{
			return;
		}
		
		f.removeAllContainerFilters();
		String txt = txtFilter.toString().trim();
		if (!txt.equals(""))
 		{
			int len = filteredColumns.length;
			
			DateStringFilter[] arr = new DateStringFilter[len];
			for (int i = 0; i < len; i++)
			{
				arr[i] = new DateStringFilter(filteredColumns[i], 
												txt, true, false);
			}
			
			f.addContainerFilter(new Or(arr));
		}
		txtFilter.focus();
	}
	
	public void setSearchShortcut()
	{
		btnSearch.setClickShortcut(KeyCode.ENTER);
	}
	
	public void removeClickShortcut()
	{
//		btnSearch.removeClickShortcut();
	}

	public void setTable(Table table)
	{
		this.table = table;
	}
	
	public Table getTable()
	{
		return table;
	}
	
	public void setFilterVisible(boolean visible){
		this.txtFilter.setVisible(visible);
		this.btnSearch.setVisible(visible);
	}
}
